<!DOCTYPE html>
<html lang="en">
<head>
    {template 'common/edithead'}
    <!--微擎 -->
    <!--这个放在最后一切平白安全-->
    <script type="text/javascript" src="./resource/js/require.js?v=20170915"></script>
    <script type="text/javascript">
        //编辑器里使用
        var urobj=[];
    </script>
</head>
<body class="nav-md" style="min-width: 800px">
<div>
    <div class="container xc_edit_from" >
        <div class="main_container" style="overflow-x: hidden;min-height: calc(100vh - 20px);background-color: white;width: 100%;">
            <div class="col-md-12 col-xs-12">
                <div class="x_panel">
                    <ul class="we7-page-tab">
                        <li >
                            <a  href="{php echo $this->createWebUrl($do, array('op'=>'list','xtitleb'=>urlencode($xtitleb),'xtitlea'=>urlencode($xtitlea),'new'=>1));}">
                                {$xtitleb}- {$xtitlea}列表  </a></li>
                        <li><a href="{php echo $this->createWebUrl($do, array('op'=>'edit','xtitleb'=>urlencode($xtitleb),'xtitlea'=>urlencode($xtitlea),'new'=>1));}">{$xtitleb}- {$xtitlea}增加</a></li>
                    </ul>
                    <div class="x_content">
                        <br/>
                        <form id="sign-form" class="form-horizontal" role="form" method="post" name="submit">
                            <input type="hidden" name="id" value="{$list['id']}">
                            <input type="hidden" name="xc[format]" value="">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">学校</label>
                                <div class="col-sm-8">
                                    <select  class="form-control js-select2" name="xc[store]" data-select="{$list['store']}">
                                        <option value="">请选择学校</option>
                                        {loop $store $index $item}
                                        <option value="{$item['id']}" {if $item['id']==$list['store']}selected{/if} >{$item['name']}</option>
                                        {/loop}
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">活动名称</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control"  name="xc[name]" value="{$list['name']}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">封面</label>
                                <div class="col-sm-8">
                                    {php echo tpl_form_field_image('xc[simg]',$list['simg']);}
                                    <span class="help-block">350*150</span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-sm-2">图片</label>
                                <div class="col-sm-8">
                                    {php echo tpl_form_field_multi_image('xc[bimg]',$list[bimg]);}
                                    <span class="help-block">375*150</span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">人数</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control"  name="xc[member]" value="{$list['member']}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">活动时间</label>
                                <div class="col-sm-8">
                                    {php echo tpl_form_field_daterange('times',$list['times'],true);}
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">活动规则</label>
                                <div class="col-sm-8">
                                    <textarea class="form-control" rows="5" name="xc[rules]">{$xc['rules']}</textarea>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">咨询电话</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control"  name="xc[mobile]" value="{$list['mobile']}">
                                </div>
                            </div>
                            <div class="form-group param" data-name="xc[format]">
                                <label  class="col-sm-2 control-label">组别</label>
                                <div class="col-sm-8">
                                    {if $list['format']}
                                    {loop $list['format'] $index $item}
                                    <div class="input-group">
                                         <span class="input-group-btn">
                                             <button class="btn btn-default" type="button">名称</button>
                                         </span>
                                        <input type="text" class="form-control" data-name="name" value="{$item['name']}">
                                        <span class="input-group-btn">
                                             <button class="btn btn-default" type="button">价格</button>
                                         </span>
                                        <input type="text" class="form-control" data-name="price" value="{$item['price']}">
                                        <span class="input-group-btn" onclick="parameter.parameter_add(this)">
                                            <button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_del(this)">
                                            <button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>
                                        </span>
                                    </div>
                                    {/loop}
                                    {else}
                                    <div class="input-group">
                                         <span class="input-group-btn">
                                             <button class="btn btn-default" type="button">名称</button>
                                         </span>
                                        <input type="text" class="form-control" data-name="name" value="">
                                        <span class="input-group-btn">
                                             <button class="btn btn-default" type="button">价格</button>
                                         </span>
                                        <input type="text" class="form-control" data-name="price" value="">
                                        <span class="input-group-btn" onclick="parameter.parameter_add(this)">
                                            <button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_del(this)">
                                            <button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>
                                        </span>
                                    </div>
                                    {/if}
                                </div>
                            </div>
                            <script>
                                var parameter={
                                    parameter_add:function(objc){
                                        $(objc).parent().after('<div class="input-group">' +
                                                '<span class="input-group-btn">' +
                                                '<button class="btn btn-default" type="button">名称</button>' +
                                                '</span>' +
                                                '<input type="text" class="form-control" data-name="name" value="">' +
                                                '<span class="input-group-btn">' +
                                                '<button class="btn btn-default" type="button">价格</button>' +
                                                '</span>' +
                                                '<input type="text" class="form-control" data-name="price" value="">' +
                                                '<span class="input-group-btn" onclick="parameter.parameter_add(this)">' +
                                                '<button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>' +
                                                '</span>' +
                                                '<span class="input-group-btn" onclick="parameter.parameter_del(this)">' +
                                                '<button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>' +
                                                '</span>' +
                                                '</div>')
                                    },
                                    parameter_del:function(objc){
                                        if($(objc).parent().siblings().length>0){
                                            $(objc).parent().remove();
                                        }
                                    }
                                }
                            </script>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">状态</label>
                                <div class="col-sm-8">
                                    <input type="checkbox" class="js-switch" value="1" name="xc[status]"
                                           data-value="{$list['status']}" data-field="status">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">排序</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control"  name="xc[sort]" value="{$list['sort']}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="control-label col-sm-2">详情</label>
                                <div class="col-sm-8">
                                    {php echo tpl_ueditor('xc[content]',$list['content']);}
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <input type="button" name="submit" class="btn btn-default" value="提交">
                                    <a type="button" class="btn btn-default"  href="{php echo $this->createWebUrl($do, array('op'=>'list','xtitleb'=> urlencode($xtitleb),'xtitlea'=>urlencode($xtitlea),'new'=>1));}" >返回</a>
                                    <input id="res" name="res" type="reset" style="display:none;" />
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>
{template 'common/editfoot'}
<script>
    $(function(){
        $("input[name='submit']").click(function(){
            getJson();
            var data=$(".form-horizontal").serialize();
            $.ajax({
                type:"post",
                url:"{url 'site/entry/'.$_GPC['do'],array('m'=>$_GPC['m'],'op'=>'savemodel','version_id'=>$_GPC['version_id'])}",
                data:data,
                dataType:'json',
                success:function(res){
                    if(res.status==1){
                        if($("input[name='id']").val()==""){
                            $("input[name='res']").click();
                            $("body").find(".img-responsive.img-thumbnail").attr("src","");
                        }
                        swal('操作成功!', '操作成功!', 'success');
                    }else{
                        swal('操作失败!', '操作失败!', 'error');
                    }
                }
            })
        });
    });

    function getJson(){
        $(".param").each(function(){
            var list=[];
            var name=$(this).attr("data-name");
            $(this).find(".input-group").each(function(){
                var data={};
                $(this).find("input").each(function(){
                    if($(this).val()!=""){
                        data[$(this).attr("data-name")]=$(this).val();
                    }
                });
                if(JSON.stringify(data)!="{}"){
                    list.push(data);
                }
            });
            if(list.length>0){
                $("input[name='"+name+"']").val(JSON.stringify(list));
            }
        });
    }
</script>
</body>
</html>